//
//  NewsDetailObj.m
//
//  Created by lizhenqiang  on 16/4/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "NewsDetailObj.h"
#import "NewsDetailDetail.h"


NSString *const kNewsDetailObjDetail = @"detail";
NSString *const kNewsDetailObjImages = @"images";


@interface NewsDetailObj ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation NewsDetailObj

@synthesize detail = _detail;
@synthesize images = _images;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.detail = [NewsDetailDetail modelObjectWithDictionary:[dict objectForKey:kNewsDetailObjDetail]];
            self.images = [self objectOrNilForKey:kNewsDetailObjImages fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.detail dictionaryRepresentation] forKey:kNewsDetailObjDetail];
    NSMutableArray *tempArrayForImages = [NSMutableArray array];
    for (NSObject *subArrayObject in self.images) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForImages addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForImages addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForImages] forKey:kNewsDetailObjImages];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.detail = [aDecoder decodeObjectForKey:kNewsDetailObjDetail];
    self.images = [aDecoder decodeObjectForKey:kNewsDetailObjImages];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_detail forKey:kNewsDetailObjDetail];
    [aCoder encodeObject:_images forKey:kNewsDetailObjImages];
}

- (id)copyWithZone:(NSZone *)zone
{
    NewsDetailObj *copy = [[NewsDetailObj alloc] init];
    
    if (copy) {

        copy.detail = [self.detail copyWithZone:zone];
        copy.images = [self.images copyWithZone:zone];
    }
    
    return copy;
}


@end
